//@version=5
indicator("AI-Like Trader Pro (Refined)", overlay=true, max_lines_count=500, max_labels_count=500)

// === INPUTS ===
rsiLen     = input.int(14, "RSI Length")
macdFast   = input.int(12, "MACD Fast EMA")
macdSlow   = input.int(26, "MACD Slow EMA")
macdSignal = input.int(9,  "MACD Signal")
emaFastLen = input.int(20, "EMA Fast Length")
emaSlowLen = input.int(50, "EMA Slow Length")
atrMult    = input.float(1.5, "ATR Multiplier")
htf        = input.timeframe("240", "Higher Timeframe (Confirm)")  // faster than daily for quicker signals

// === INDICATORS ===
price = close
rsi = ta.rsi(price, rsiLen)
[macd, macdSig, _] = ta.macd(price, macdFast, macdSlow, macdSignal)
emaFast = ta.ema(price, emaFastLen)
emaSlow = ta.ema(price, emaSlowLen)
atr = ta.atr(14)
htf_rsi = request.security(syminfo.tickerid, htf, ta.rsi(price, rsiLen))

// === CONDITIONS ===
// Trend check
trendUp   = emaFast > emaSlow
trendDown = emaFast < emaSlow

// RSI momentum
rsi_up   = ta.crossover(rsi, 30)  // fires as RSI leaves oversold
rsi_down = ta.crossunder(rsi, 70) // fires as RSI leaves overbought

// MACD momentum
macd_up   = ta.crossover(macd, macdSig)
macd_down = ta.crossunder(macd, macdSig)

// HTF confirmation
htf_up   = htf_rsi > htf_rsi[1]
htf_down = htf_rsi < htf_rsi[1]

// === COMBINED SIGNALS ===
confidence_buy  = (trendUp   ? 1 : 0) + (rsi_up   ? 1 : 0) + (macd_up   ? 1 : 0) + (htf_up   ? 1 : 0)
confidence_sell = (trendDown ? 1 : 0) + (rsi_down ? 1 : 0) + (macd_down ? 1 : 0) + (htf_down ? 1 : 0)

buy_trigger  = confidence_buy  >= 2  // fires faster (only 2/4 conditions needed)
sell_trigger = confidence_sell >= 2

// === TRADE STATE ===
var bool inLong  = false
var bool inShort = false

buy_signal  = false
sell_signal = false
exit_signal = false

if buy_trigger and not inLong
    buy_signal := true
    inLong  := true
    inShort := false
    exit_signal := false

if sell_trigger and not inShort
    sell_signal := true
    inShort := true
    inLong  := false
    exit_signal := false

// === EXIT RULES ===
// Exit Long if price closes below stop or opposite trigger fires
long_stop = inLong ? price - atrMult * atr : na
if inLong and (close < long_stop or sell_trigger)
    exit_signal := true
    inLong := false

// Exit Short if price closes above stop or opposite trigger fires
short_stop = inShort ? price + atrMult * atr : na
if inShort and (close > short_stop or buy_trigger)
    exit_signal := true
    inShort := false

// === PLOTTING ===
plotshape(buy_signal,  title="BUY",  location=location.belowbar, color=color.green, style=shape.triangleup,   size=size.small, text="BUY")
plotshape(sell_signal, title="SELL", location=location.abovebar, color=color.red,   style=shape.triangledown, size=size.small, text="SELL")
plotshape(exit_signal, title="EXIT", location=location.belowbar, color=color.yellow, style=shape.circle, size=size.tiny, text="EXIT")

plot(long_stop,  color=color.red,   style=plot.style_linebr, title="Long Stop")
plot(short_stop, color=color.green, style=plot.style_linebr, title="Short Stop")

// === ALERTS ===
alertcondition(buy_signal,  title="Buy Alert",  message="AI-Like Trader Pro: BUY signal!")
alertcondition(sell_signal, title="Sell Alert", message="AI-Like Trader Pro: SELL signal!")
alertcondition(exit_signal, title="Exit Alert", message="AI-Like Trader Pro: EXIT trade!")

plot(close)
